/**
 * Created by Jackie.Chen on 2017/5/12.
 */
import Vue from 'vue';
import VueRouter from 'vue-router';
import Axios from '../api/api'
Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    { path: '/',
      redirect: '/login',
      meta: {noCheckSession: true}
    },
    {
      path: '/test',
      name: 'test',
      component: function(reslove){
        return require(['../components/test/test'], reslove)
      }
    },
    {
      path: '/bar',
      name: 'bar',
      component: function (reslove) {
        return require(['../components/test/test2'], reslove)
      }
    },
    {
      path: '/product',
      component:function (reslove) {
        return require(['../components/product/product'], reslove)
      },
      children: [
        //模型概况
        {
          path: "situation",
          component: function (reslove) {
            return require(['../components/product/situation/situation'], reslove)
          }
        },
        //模型基本信息添加
        {
          path: "addModelBasic",
          component: function (reslove) {
            return require(['../components/product/modelType/model_basic'], reslove)
          }
        },
        //模型基本信息编辑
        {
          path: "editModelBasic",
          component: function (reslove) {
            return require(['../components/product/modelType/model_basic_edit'], reslove)
          }
        },
        //模型算法设置
        {
          path: "modelAlgorithm",
          component: function (reslove) {
            return require(['../components/product/modelType/model_algorithm'], reslove)
          }
        },
        //上传数据
        {
          path: "modelImportData",
          component: function (reslove) {
            return require(['../components/product/datatype/model_import_data'], reslove)
          }

        },
        //上传数据
        {
          path: "modelImportDataEdit",
          component: function (reslove) {
            return require(['../components/product/datatype/model_import_data_edit'], reslove)
          }
        },
        //数据探索
        {
          path: "modelDataExplore",
          component: function (reslove) {
            return require(['../components/product/modelType/model_data_explore'], reslove)
          }
        },
        //使用模型-数据探索
        {
          path: "modelUseDataExplore",
          component: function (reslove) {
            return require(['../components/product/modelType/model_use_data_explore'], reslove)
          }
        },
        //模型列表
        {
          path: "model-list",
          component: function (reslove) {
            return require(['../components/product/modelType/model-list'], reslove)
          }
        },
        //1.0的模型列表
        {
          path: "moModel",
          component: function (reslove) {
            return require(['../components/product/modelType/model'], reslove)
          }
        },
        //我的轨迹
        {
          path: "myTrail",
          component: function (reslove) {
            return require(['../components/product/history/history'], reslove)
          }
        },
        //用户中心
        {
          path: "personCenter",
          component: function (reslove) {
            return require(['../components/product/situation/situation'], reslove)
          }
        },
        //模型详情
        {
          path: "modelDe",
          component: function (reslove) {
            return require(['../components/product/situation/model-detail'], reslove)
          }
        },
        {
          path: "dataExplore",
          component: function (reslove) {
            return require(['../components/product/modelType/explore'], reslove)
          }
        },
          {
              path: "dataType",
              component: function (reslove) {
                  return require(['../components/product/datatype/updata'], reslove)
              }
          },
        {
          path: "modelEva",
          component: function (reslove) {
            return require(['../components/product/modelType/model-evalate'], reslove)
          }
        },
        {
          path: "result",
          component: function (reslove) {
            return require(['../components/product/modelType/result'], reslove)
          }
        },
        {
          path: "export-result",
          component: function (reslove) {
            return require(['../components/product/modelType/export-result'], reslove)
          }
        },
        {
          path: "show-setting",
          component: function (reslove) {
            return require(['../components/product/modelType/showSetting'], reslove)
          }
        },
        //算法设置
        {
          path: "algorithmSet",
          component: function (reslove) {
            return require(['../components/product/modelType/algorithm_set'], reslove)
          }
        },
        //算法设置
        {
          path: "algorithmSetEdit",
          component: function (reslove) {
            return require(['../components/product/modelType/algorithm_set_edit'], reslove)
          }
        },
        //输入项设置
        {
          path: "inputSet",
          component: function (reslove) {
            return require(['../components/product/modelType/input_set'], reslove)
          }
        }
      ]
    }
  ]
});
router.beforeEach((to, from, next) => {
  if(to.matched.some(record => !record.meta.noCheckSession))
{
  Axios.get('/user/getUserInfo.do').then(function(res) {
    //获取用户信息
    console.log('获取用户信息请求后返回结果: ', res);
    if(res.data.status === 'OK') {
      if(res.data.result){
        var userInfo = res.data.result;
        localStorage.setItem("userId",userInfo.userid);
        localStorage.setItem("token",userInfo.tokenValue);
        if (!localStorage.getItem("userId")) {
          layer.confirm('请先登陆', {
            btn: ['确定'] //按钮
          }, function (index) {
            layer.close(index);
            location.href = "/user/login.html";
          });
        }else{
          next();
        }
      }else{
        layer.msg('登录超时，获取用户信息失败，请重新登录！');
      }
    }else {
      layer.msg('登录超时，获取用户信息失败，请重新登录!');
    }
  });
}else{
  next();
  localStorage.setItem('from', from.path);
}
})

export default router;
